package com.sinaapp.zygl.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import com.sinaapp.zygl.bean.User;
import com.sinaapp.zygl.dao.UserDao;
import com.sinaapp.zygl.dao.impl.UserDaoImpl;

public class UserAction extends ActionSupport {

	private static final long serialVersionUID = -2660072548797753754L;

	@Override
	public String execute() throws Exception {
		UserDao dao = new UserDaoImpl();
		List<User> upagelist = new ArrayList<User>();
		User user = new User();
		user.setId(id);
		user.setUid(uid);
		user.setName(name);
		user.setPwd(pwd);
		user.setType(type);
		user.setCreatedatetimeStart(createdatetimeStart);
		user.setCreatedatetimeEnd(createdatetimeEnd);
		user.setModifydatetimeStart(modifydatetimeStart);
		user.setModifydatetimeEnd(modifydatetimeEnd);
		upagelist = dao.getUsers(page, rows, sort, order, user);
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("total", dao.queryUserTotal(user));
		m.put("rows", upagelist);
		writeJson(m);
		return null;
	}

	public String reg() throws Exception {
		UserDao dao = new UserDaoImpl();
		User user = new User();
		user.setName(name);
		user.setPwd(pwd);
		user.setType(type);
		;
		Map<String, Object> m = new HashMap<String, Object>();
		if (dao.register(user)) {
			m.put("user", user);
			m.put("success", true);
		} else {
			m.put("user", user);
			m.put("success", false);
		}
		writeJson(m);
		return null;
	}

	public String update() throws Exception {
		UserDao dao = new UserDaoImpl();
		User user = new User();
		user.setUid(uid);
		user.setName(name);
		user.setPwd(pwd);
		user.setType(type);
		user.setCreatedatetime(createdatetime);
		Map<String, Object> m = new HashMap<String, Object>();
		if (dao.update(user)) {
			m.put("user", user);
			m.put("success", true);
		} else {
			m.put("user", user);
			m.put("success", false);
		}
		writeJson(m);
		return null;
	}

	public String delete() throws Exception {
		UserDao dao = new UserDaoImpl();
		Map<String, Object> m = new HashMap<String, Object>();
		if (dao.delete(id)) {
			m.put("id", id);
			m.put("success", true);
		} else {
			m.put("id", id);
			m.put("success", false);
		}
		writeJson(m);
		return null;
	}

	public void writeJson(Object o) {
		// String json = getJsonString(o);
		try {
			// ServletActionContext.getResponse().getWriter().write(json);
			String json = JSON.toJSONStringWithDateFormat(o, "yyyy-MM-dd HH:mm:ss");
			ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
			ServletActionContext.getResponse().getWriter().close();
		} catch (Exception e) {
		}
	}

	private int page;
	private int rows;
	private String sort;
	private String order;

	private int id;
	private String uid;
	private String name;
	private String pwd;
	private String type;
	private Date createdatetime;
	private Date modifydatetime;

	private Date createdatetimeStart;
	private Date createdatetimeEnd;
	private Date modifydatetimeStart;
	private Date modifydatetimeEnd;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Date getCreatedatetime() {
		return createdatetime;
	}

	public void setCreatedatetime(Date createdatetime) {
		this.createdatetime = createdatetime;
	}

	public Date getCreatedatetimeStart() {
		return createdatetimeStart;
	}

	public void setCreatedatetimeStart(Date createdatetimeStart) {
		this.createdatetimeStart = createdatetimeStart;
	}

	public Date getCreatedatetimeEnd() {
		return createdatetimeEnd;
	}

	public void setCreatedatetimeEnd(Date createdatetimeEnd) {
		this.createdatetimeEnd = createdatetimeEnd;
	}

	public Date getModifydatetime() {
		return modifydatetime;
	}

	public void setModifydatetime(Date modifydatetime) {
		this.modifydatetime = modifydatetime;
	}

	public Date getModifydatetimeStart() {
		return modifydatetimeStart;
	}

	public void setModifydatetimeStart(Date modifydatetimeStart) {
		this.modifydatetimeStart = modifydatetimeStart;
	}

	public Date getModifydatetimeEnd() {
		return modifydatetimeEnd;
	}

	public void setModifydatetimeEnd(Date modifydatetimeEnd) {
		this.modifydatetimeEnd = modifydatetimeEnd;
	}

}
